package com.hy.demo.learn.spring.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *
 * </p>
 *
 * @author cjl
 * @version 1.0
 * @since 2020-07-28 15:06
 */
@Slf4j
@Aspect
@Component
public class LogAspect {


    @Pointcut(value = " execution (public * com.hy.demo.learn.spring.controller..*.*(..))")
    public void webLog(){}

    /**
     * 前置通知
     */
    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint){
        ServletRequestAttributes servletRequestAttributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=servletRequestAttributes.getRequest();
        log.info("请求地址=={}",request.getRequestURL().toString());
        log.info("请求ip=={}",request.getRemoteAddr());
    }

    /**
     * 后置通知
     */
    @AfterReturning( returning = "ret",pointcut = "webLog()")
    public void doAfter(Object ret){

        log.info("响应内容=={}",ret);
    }

//    @After() 最终通知
//    @AfterThrowing() 异常通知
//    @Around() 环绕通知
}
